<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('settings.inc');
require_once('helper.inc');
require_once('crypt.inc');

$drm = array(false, false, true, true, true, true, true, true);

require_once('menu.inc');
require_once('knumber.inc');

$func = $_REQUEST['func'];

function EditTask($pt_num, $neu=false) {
	$menu = $_REQUEST['menu'];
	global $unum;
	global $rstufe;
	global $phase;
	global $numphase;

	$pt_lfd = $_REQUEST['pt_lfd'];
	$pt_level = $_REQUEST['pt_level'];
	$pt_name = $_REQUEST['pt_name'];
	$pt_color = $_REQUEST['pt_color'];
	$pt_meeting = $_REQUEST['pt_meeting'];
	$pt_phase = $_REQUEST['pt_phase'];
	$color = $_REQUEST['color'];

	if (!isset($color) || strlen($color) <= 0)
	   $color = "ffffcc";

	if ($neu) {
	   $pt_tnum = $pt_num;
	   $pt_num = 0;
	}
	else
	   $pt_tnum = $_REQUEST['pt_tnum'];

	if (!isset($pt_lfd) || $pt_lfd <= 0)
	   $pt_lfd = 1;

	$db = OpenDB();

	if (!$neu) {
	   $query = "select pt_tnum, pt_lfd, pt_level, pt_name, pt_color, ";
	   $query .= "pt_meeting, pt_phase from ptemp where pt_num = $pt_num";
	   $result = QueryDB($db, $query);

	   if (!$result) {
    	      closeDB($db);
    	      return;
    	   }

	   $numrows = numrowsDB($result);

	   if ($numrows != 1) {
	      Error("Interner Fehler: Task \"$pt_num\" existiert nicht!<br>");
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($result, 0);
	   $pt_tnum = $data[0];
	   $pt_lfd = $data[1];
	   $pt_level = $data[2];
	   $pt_name = $data[3];
	   $pt_color = $data[4];
	   $pt_meeting = $data[5];
	   $pt_phase = $data[6];
	   $color = dechex($pt_color);
	   $len = strlen($color);

	   while ($len < 6) {
	      $color = "0" . $color;
	      $len++;
	   }
	}
	else if (isset($pt_color)) {
	   $color = dechex($pt_color);
	   $len = strlen($color);

	   while ($len < 6) {
	      $color = "0" . $color;
	      $len++;
	   }
	}

	if (isset($pt_meeting) && ($pt_meeting == "t" || $pt_meeting == "f")) {
	   if ($pt_meeting == "f")
	      $pt_meeting = 0;
	   else
	      $pt_meeting = 1;
	}

	# Den Namen des Templates hinschreiben
	if (!$neu) {
	   $query = "select te_name from tempidx where te_num = $pt_tnum";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return;
 	   }

	   $data = fetchDB($result, 0);
	   $te_name = $data[0];
	   echo "<p class=\"cry\">Template: $te_name</p>";
	}
?>
<br>
<form name="Task" action="templates.php" method="post">
<input type="hidden" name="pt_num" value="<? echo "$pt_num"; ?>">
<input type="hidden" name="pt_tnum" value="<? echo "$pt_tnum"; ?>">
<input type="hidden" name="headline" value="28">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="pt_lfd" value="<? echo "$pt_lfd"; ?>">
<input type="hidden" name="func" value="SaveTask">

<table class="input">
   <tr>
      <td>Level:</td>
      <td><input type="text" class="inputmust" name="pt_level" value="<? echo "$pt_level"; ?>" size=1 maxlength=1></td>
   </tr>
   <tr>
      <td>Taskname:</td>
      <td><input type="text" class="inputmust" name="pt_name" value="<? echo "$pt_name"; ?>" size=40 maxlength=50></td>
   </tr>
   <tr>
      <td>Farbe:</td>
      <td><input type="text" name="color" value="<? echo "$color"; ?>" size=8 maxlength=6>
<?php
	echo "<button type=\"button\" onClick=\"javascript:open_window('templates.php?header=1&func=GetColor&target=window.opener.document.Task.color', 'Farbwahl', 217, 57)\">";
	echo "<img src=\"image/coloredit.png\" border=0 width=15 height=15 alt=\"Farbwahl\"></button>";
?>
      </td>
   </tr>
   <tr>
      <td>Meilenstein:</td>
<?php
	if ($pt_meeting) {
           echo "<td><input type=\"checkbox\" name=\"pt_meeting\" value=\"1\" checked></td>\n";
	} else {
           echo "<td><input type=\"checkbox\" name=\"pt_meeting\" value=\"1\"></td>\n";
	}
?>
   </tr>
<?php
	if ($phase == 1) {
	   echo "<tr><td>Phase:</td>\n<td>";
	   $query = "select kp_num, kp_phase from key_phase order by kp_num";
	   $result = QueryDB($db, $query);

	   if (!$result)
	      return;

	   $numrows = numrowsDB($result);
	   $row = 0;
	   echo "<select name=\"pt_phase\">\n";

	   while ($row < $numrows) {
	      $data = fetchDB($result, $row);
	      $kp_num = $data[0];
	      $kp_phase = $data[1];

	      if ($pt_phase == $kp_num)
	         echo "<option value=\"$kp_num\" selected>$kp_num. $kp_phase</option>\n";
	      else
	         echo "<option value=\"$kp_num\">$kp_num. $kp_phase</option>\n";

	      $row++;
	   }

	   echo "</td></tr>\n";
	}
?>
   <tr>
</table>
<table class="indent"><tr><td>
<?
ButtonSubmit("Task Speichern", "Task");
?>
</td></tr></table>
</form>
<?php
	closeDB($db);
}

function EditTemplate($te_num, $edit=true) {
	$headline = $_REQUEST['headline'];
	$menu = $_REQUEST['menu'];
	global $unum;
	global $rstufe;
	global $phase;
	global $numphase;

	$nav = "te_num=$te_num&menu=$menu&headline=$headline";

	if (!isset($te_num) || $te_num < 1) {
	   Error("Interner Fehler! Templatenummer fehlt oder ist ung&uuml;ltig!<br>");
	   return;
	}

	$db = OpenDB();
	# Den Namen des Templates hinschreiben.
	$query = "select te_name from tempidx where te_num = $te_num";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$te_name = $data[0];

	echo "<p class=\"cry\">Template: $te_name</p>\n";

	# Das Template optisch aufbauen.
	$query = "select pt_num, pt_tnum, pt_lfd, pt_level, pt_name, pt_color, ";
	$query .= "pt_meeting, pt_phase from ptemp where pt_tnum = $te_num ";
	$query .= "order by pt_lfd";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$row = 0;
	$pt_lfd = 0;

	if ($numrows > 0) {
	   $db2 = OpenDB();

/*	   if ($edit) {
	      echo "<table class=\"indent\"><tr><td>\n";
	      ButtonLink("Task Einf&uuml;gen", "templates.php", "taskadd=$pt_tnum&pt_lfd=1&$nav");
//	      echo "<a href=\"#\" onClick=\"javascript:MoveTo('template.php','taskadd=$pt_tnum&pt_lfd=1&$nav')\"><img src=\"image/button_taskinsert.png\" border=0 alt=\"Einfuegen\"></a>\n";
	      echo "</td></tr></table>\n";
	   }
*/
?>
<table class="sel">
   <tr>
<?php
	   if ($edit)
	      echo "<th class=\"sel\">Aktion</th>\n";
?>
      <th class="sel">Nr.</center></th>
      <th class="sel">Taskname</th>
      <th class="sel">Typ</th>
   </tr>
<?php
	   $snum = "1";
	   $olevel = 0;

	   while ($row < $numrows) {
	      $data = fetchDB($result, $row);
	      $pt_num = $data[0];
	      $pt_tnum = $data[1];
	      $pt_lfd = $data[2];
	      $pt_level = $data[3];
	      $pt_name = $data[4];
	      $pt_color = $data[5];

	      if ($data[6] == "t")
	         $pt_meeting = 1;
	      else
		 $pt_meeting = 0;

	      $pt_phase = $data[7];

	      $query = "select pt_level from ptemp where pt_tnum = $te_num and pt_lfd > $pt_lfd order by pt_lfd";
	      $result2 = QueryDB($db2, $query);
	      $nr = numrowsDB($result2);

	      if ($nr > 0) {
	         $data = fetchDB($result2, 0);
		 $xlevel = $data[0];
	      } else {
	         $xlevel = $pt_level;
	      }

	      if ($olevel == 0) {
	         $level = 0;
		 $olevel = $pt_level;
		 $dir = -3;
	      } else if ($olevel < $pt_level) {
	         $level = $pt_level - 1;
		 $olevel = $pt_level;
		 $dir = -3;
	      } else if ($olevel == $pt_level) {
	         $level = $pt_level - 1;
		 $dir = 0;
	      } else if ($olevel > $pt_level) {
		 $level = $pt_level - 1;
		 $olevel = $pt_level;
		 $dir = -1;
	      }

	      echo "<tr>";

	      if ($edit) {
	         echo "<td class=\"selakt\"><table border=0 cellspacing=0 cellpadding=0><tr>\n";
	         echo "<td><a href=\"#\" onClick=\"javascript:MoveTo('templates.php','taskadd=$pt_tnum&pt_lfd=$pt_lfd&$nav')\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></a></td>\n";
	         echo "<td><a href=\"#\" onClick=\"javascript:MoveTo('templates.php','edittask=$pt_num&$nav')\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></a></td>\n";
	         echo "<td><a href=\"#\" onClick=\"javascript:MoveTo('templates.php','deletetask=$pt_num&pt_tnum=$pt_tnum&$nav')\"><img src=\"image/editdelete.png\" border=0 alt=\"Delete\"></a></td>\n";
	         echo "</tr></table>\n";
	         echo "</td>\n";
	      }

	      $snum = kNumber ($snum, $level, $dir);

	      if ($phase == 1 && $numphase == 1 && isset($pt_phase)) {
	         if (strpos($snum, ".")) {
	            $sarr = explode(".", $snum);
	            $sarr[0] = (string)$pt_phase;
	            $snum = implode(".", $sarr);
		 } else {
		    $snum = (string)$pt_phase;
		 }
	      }

	      echo "<td class=\"sel\">$snum</td>\n<td class=\"sel\">$pt_name</td>\n";

	      if ($pt_meeting) {
	         echo "<td class=\"sel\"><b>Meilenstein</b></td>\n";
	      } else if ($pt_level == 1 || $xlevel > $pt_level) {
	         echo "<td class=\"sel\"><i>Ordner</i></td>\n";
	      } else {
	         echo "<td class=\"sel\">Task</td>\n";
	      }

	      echo "</tr>\n";
	      $row++;
	   }

	   $pt_lfd++;
	   echo "</table>\n";

	   if ($edit) {
	      echo "<table class=\"indent\"><tr><td>\n";
	      ButtonLink("Task Anh&auml;ngen", "templates.php", "taskadd=$pt_tnum&pt_lfd=$pt_lfd&$nav");
	      echo "</td></tr></table>\n";
//	      echo "<a href=\"#\" onClick=\"javascript:MoveTo('templates.php','taskadd=$pt_tnum&pt_lfd=$pt_lfd&$nav')\"><img src=\"image/button_taskappend.png\" border=0 alt=\"Einfuegen\"></a>\n";
	   }

	   echo "</form>\n";
	   closeDB($db);
	   closeDB($db2);
	} else {
	   closeDB($db);
	   EditTask($te_num, true);
	}
}

function SaveTask() {
	global $phase;
	$pt_num = $_REQUEST['pt_num'];
	$pt_tnum = $_REQUEST['pt_tnum'];
	$pt_lfd = $_REQUEST['pt_lfd'];
	$pt_level = $_REQUEST['pt_level'];
	$pt_name = $_REQUEST['pt_name'];
	$pt_color = $_REQUEST['pt_color'];
	$color = $_REQUEST['color'];
	$pt_meeting = $_REQUEST['pt_meeting'];
	$pt_phase = $_REQUEST['pt_phase'];

	# Plausibilitaetspruefungen
	$err = 0;

	if (!isset($pt_tnum) || $pt_tnum < 1) {
	   Error("Interner Fehler: Template-Indexnummer fehlt!<br>");
	   $err = 1;
	}

	if (!isset($pt_lfd) || $pt_lfd < 1) {
	   Error("Interner Fehler: Keine oder ung&uuml;ltige ID-Nummer!<br>");
	   $err = 1;
	}

	if (strlen($pt_name) < 1) {
	   Error("Ein Task muss einen Namen haben!<br>");
	   $err = 1;
	}

	if (!isset($pt_color) && strlen($color) != 6) {
	   Error("Ungueltiger Farbcode!<br>");
	   $err = 1;
	}

	if ($err == 1) {
	   return false;
	}

	# Umwandlungen
	if (!isset($pt_color))
	   $pt_color = hexdec($color);

	if (!isset($pt_meeting))
	   $pt_meeting = 0;

	$db = OpenDB();

	# Aendern wir einen bestehenden Datensatz, oder speichern wir einen
	# neuen?
	if (isset($pt_num) && $pt_num > 0) {		// neuer Datensatz?
	   $query = "select pt_num from ptemp where pt_num = $pt_num";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return false;
	   }

	   $numrows = numrowsDB($result);

	   # Gibt es den Datensatz bereits, dann aendern wir ihn.
	   if ($numrows == 1) {
	      $query = "update ptemp set pt_name = '$pt_name', pt_color = '$pt_color',";
	      $query .= "pt_meeting = '$pt_meeting', pt_level = $pt_level,";

	      if ($phase == 1)
	         $query .= "pt_tnum = $pt_tnum, pt_lfd = $pt_lfd, pt_phase = $pt_phase ";
	      else
	         $query .= "pt_tnum = $pt_tnum, pt_lfd = $pt_lfd ";

	      $query .= "where pt_num = $pt_num";

	      if (!TQueryDB($db, $query)) {
		 closeDB($db);
		 return false;
	      }

	      closeDB($db);
	      return true;
	   }
	}

	# Der Datensatz existiert nicht und deshalb fuegen wir ihn ein. Es
	# muss jedoch geprueft werden, ob die ID bereits existiert. Existiert
	# sie, muessen alle IDs >= $pt_lfd um 1 erhoeht werden und der
	# Datensatz anschliessend eingefuegt werden.
	$query = "select pt_lfd from ptemp where pt_tnum = $pt_tnum and pt_lfd = $pt_lfd";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return false;
	}

	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   # Alle IDs um 1 nach hinten schieben!
	   $query = "select pt_num, pt_lfd from ptemp where pt_tnum = $pt_tnum and pt_lfd >= $pt_lfd order by pt_lfd desc";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return false;
	   }

	   $numrows = numrowsDB($result);
	   $row = 0;
	   $db2 = OpenDB();

	   while ($row < $numrows) {
	      $data = fetchDB($result, $row);
	      $num = $data[0];
	      $id = $data[1] + 1;
	      $query = "update ptemp set pt_lfd = $id where pt_num = $num";

	      if (!TQueryDB($db2, $query)) {
		 closeDB($db);
		 closeDB($db2);
		 return false;
	      }

	      $row++;
	   }

	   closeDB($db2);
	}

	# Nun wird der neue Datensatz tatsaechlich geschrieben.
	$query = "select co_ptemp from counter";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return false;
	}

	$data = fetchDB($result, 0);
	$pt_num = $data[0] + 1;

	QueryDB($db, "begin");
	$query = "insert into ptemp (pt_num, pt_tnum, pt_lfd, ";
	$query .= "pt_name, pt_color, pt_meeting,";
	$query .= "pt_level, pt_phase) values (";
	$query .= "$pt_num, $pt_tnum, $pt_lfd, ";
	$query .= "'$pt_name', $pt_color, ";
	$query .= "'$pt_meeting', ";

	if ($phase == 1)
	   $query .= "$pt_level, $pt_phase)";
	else
	   $query .= "$pt_level, 0)";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	$query = "update counter set co_ptemp = $pt_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	QueryDB($db, "commit");
	closeDB($db);
	return true;
}

function AskDeleteTask($pt_num) {
	$headline = $_REQUEST['headline'];
	$menu = $_REQUEST['menu'];
	$pt_tnum = $_REQUEST['pt_tnum'];
	$nav = "pt_num=$pt_num&pt_tnum=$pt_tnum&func=DeleteTask&menu=$menu&headline=28";

	$db = OpenDB();
	$query = "select te_name from tempidx where te_num = $pt_tnum";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$te_name = $data[0];
	echo "<p class=\"cry\">Template: $te_name</p>";
	closeDB($db);
?>
<br>
<form action="templates.php" method="post">
<table border=2 class="alarm">
   <tr><td colspan=2><big>Warnung!</big><br><br>
           Sie sind im Begriff einen Task zu l&ouml;schen!<br><br>
	   Wollen Sie wirklich diesen Task unwiederbringlich l&ouml;schen?<br>
      </td>
   </tr>
   <tr>
      <td><center><input type="button" name="yes" value="L&ouml;schen" onClick="javascript:MoveTo('templates.php','yes=yes&<? echo "$nav"; ?>')"></center></td>
      <td><center><input type="button" name="no" value="Abbruch" onClick="javascript:MoveTo('templates.php','no=no&<? echo "$nav"; ?>')"></center></td>
   </tr>
</table>
</form>
<?php
}

function DeleteTask() {
	$pt_num = $_REQUEST['pt_num'];

	if (!isset($pt_num) || $pt_num <= 0) {
	   Error("Error: Interner Fehler: Ung&uuml;ltige oder fehlende Seriennummer!");
	   return false;
	}

	$db = OpenDB();
	QueryDB($db, "begin");
	$query = "delete from ptemp where pt_num = $pt_num";
	$result = QueryDB($db, $query);

	if (!$result) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	QueryDB($db, "commit");
	closeDB($db);
	return true;
}

function GetColor() {
	$target = $_REQUEST['target'];
?>
<map name="farbe">
<area shape="rect" coords="1,1,26,10" href="javascript:Farbe('255','128','128','<? echo "$target"; ?>')">
<area shape="rect" coords="28,1,53,10" href="javascript:Farbe('255','255','128','<? echo "$target"; ?>')">
<area shape="rect" coords="55,1,80,10" href="javascript:Farbe('128','255','128','<? echo "$target"; ?>')">
<area shape="rect" coords="82,1,107,10" href="javascript:Farbe('0','255','128','<? echo "$target"; ?>')">
<area shape="rect" coords="109,1,134,10" href="javascript:Farbe('128','255','255','<? echo "$target"; ?>')">
<area shape="rect" coords="136,1,161,10" href="javascript:Farbe('0','128','255','<? echo "$target"; ?>')">
<area shape="rect" coords="163,1,188,10" href="javascript:Farbe('255','128','192','<? echo "$target"; ?>')">
<area shape="rect" coords="190,1,215,10" href="javascript:Farbe('255','128','255','<? echo "$target"; ?>')">
<area shape="rect" coords="1,12,26,21" href="javascript:Farbe('255','0','0','<? echo "$target"; ?>')">
<area shape="rect" coords="28,12,53,21" href="javascript:Farbe('255','255','0''<? echo "$target"; ?>')">
<area shape="rect" coords="55,12,80,21" href="javascript:Farbe('128','255','0','<? echo "$target"; ?>')">
<area shape="rect" coords="82,12,107,21" href="javascript:Farbe('0','255','64','<? echo "$target"; ?>')">
<area shape="rect" coords="109,12,134,21" href="javascript:Farbe('0','255','255','<? echo "$target"; ?>')">
<area shape="rect" coords="136,12,161,21" href="javascript:Farbe('0','128','192','<? echo "$target"; ?>')">
<area shape="rect" coords="163,12,188,21" href="javascript:Farbe('128','128','192','<? echo "$target"; ?>')">
<area shape="rect" coords="190,12,215,21" href="javascript:Farbe('255','0','255','<? echo "$target"; ?>')">
<area shape="rect" coords="1,23,26,32" href="javascript:Farbe('128','64','64','<? echo "$target"; ?>')">
<area shape="rect" coords="28,23,53,32" href="javascript:Farbe('255','128','64','<? echo "$target"; ?>')">
<area shape="rect" coords="55,23,80,32" href="javascript:Farbe('0','255','0','<? echo "$target"; ?>')">
<area shape="rect" coords="82,23,107,32" href="javascript:Farbe('0','128','128','<? echo "$target"; ?>')">
<area shape="rect" coords="109,23,134,32" href="javascript:Farbe('0','64','128','<? echo "$target"; ?>')">
<area shape="rect" coords="136,23,161,32" href="javascript:Farbe('128','128','255','<? echo "$target"; ?>')">
<area shape="rect" coords="163,23,188,32" href="javascript:Farbe('128','0','64','<? echo "$target"; ?>')">
<area shape="rect" coords="190,23,215,32" href="javascript:Farbe('255','0','128','<? echo "$target"; ?>')">
<area shape="rect" coords="1,34,26,43" href="javascript:Farbe('128','0','0','<? echo "$target"; ?>')">
<area shape="rect" coords="28,34,53,43" href="javascript:Farbe('255','128','0','<? echo "$target"; ?>')">
<area shape="rect" coords="55,34,80,43" href="javascript:Farbe('0','128','0','<? echo "$target"; ?>')">
<area shape="rect" coords="82,34,107,43" href="javascript:Farbe('0','128','64','<? echo "$target"; ?>')">
<area shape="rect" coords="109,34,134,43" href="javascript:Farbe('0','0','255','<? echo "$target"; ?>')">
<area shape="rect" coords="136,34,161,43" href="javascript:Farbe('0','0','160','<? echo "$target"; ?>')">
<area shape="rect" coords="163,34,188,43" href="javascript:Farbe('128','0','128','<? echo "$target"; ?>')">
<area shape="rect" coords="190,34,215,43" href="javascript:Farbe('128','0','255','<? echo "$target"; ?>')">
<area shape="rect" coords="1,45,26,54" href="javascript:Farbe('64','0','0','<? echo "$target"; ?>')">
<area shape="rect" coords="28,45,53,54" href="javascript:Farbe('128','64','0','<? echo "$target"; ?>')">
<area shape="rect" coords="55,45,80,54" href="javascript:Farbe('0','64','0','<? echo "$target"; ?>')">
<area shape="rect" coords="82,45,107,54" href="javascript:Farbe('0','64','64','<? echo "$target"; ?>')">
<area shape="rect" coords="109,45,134,54" href="javascript:Farbe('0','0','128','<? echo "$target"; ?>')">
<area shape="rect" coords="136,45,161,54" href="javascript:Farbe('0','0','64','<? echo "$target"; ?>')">
<area shape="rect" coords="163,45,188,54" href="javascript:Farbe('64','0','64','<? echo "$target"; ?>')">
<area shape="rect" coords="190,45,215,54" href="javascript:Farbe('64','0','128','<? echo "$target"; ?>')">
<area shape="rect" coords="1,56,26,65" href="javascript:Farbe('0','0','0','<? echo "$target"; ?>')">
<area shape="rect" coords="28,56,53,65" href="javascript:Farbe('128','128','0','<? echo "$target"; ?>')">
<area shape="rect" coords="55,56,80,65" href="javascript:Farbe('128','128','64','<? echo "$target"; ?>')">
<area shape="rect" coords="82,56,107,65" href="javascript:Farbe('128','128','128','<? echo "$target"; ?>')">
<area shape="rect" coords="109,56,134,65" href="javascript:Farbe('64','128','128','<? echo "$target"; ?>')">
<area shape="rect" coords="136,56,161,65" href="javascript:Farbe('192','192','192','<? echo "$target"; ?>')">
<area shape="rect" coords="163,56,188,65" href="javascript:Farbe('64','64','64','<? echo "$target"; ?>')">
<area shape="rect" coords="190,56,215,65" href="javascript:Farbe('255','255','255','<? echo "$target"; ?>')">
</map>
<img src="image/farben.png" width="217" height="67" border="0" vspace="3" alt="" usemap="#farbe">
<form name="schliessen">
   <input type="button" name="close" value="Schliessen" onClick="javascript:self.close()">
</form>
<?php
}

function TempNew($te_num=0) {
	$menu = $_REQUEST['menu'];
	$te_name = $_REQUEST['te_name'];
	$te_zweck = $_REQUEST['te_zweck'];

	if ($te_num > 0) {
	   $db = OpenDB();
	   $query = "select te_name, te_zweck from tempidx where te_num = $te_num";
	   $result = QueryDB($db, $query);

	   if (!$result) {
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($result, 0);
	   $te_name = $data[0];
	   $te_zweck = $data[1];
	   closeDB($db);
	}
?>
<form name="Temp" action="templates.php" method="post">
   <input type="hidden" name="headline" value="28">
   <input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<?php
	if ($te_num > 0) {
	   echo "<input type=\"hidden\" name=\"te_num\" value=\"$te_num\">\n";
	   echo "<input type=\"hidden\" name=\"func\" value=\"tempupdate\">\n";
	} else
	   echo "<input type=\"hidden\" name=\"func\" value=\"tempsave\">\n";
?>
   <input type="hidden" name="pt_lfd" value="1">

   <table class="input">
      <tr>
         <td>Template Name:</td>
         <td><input type="text" class="inputmust" name="te_name" value="<? echo "$te_name"; ?>" size=40 maxlength=60></td>
      </tr>
      <tr>
         <td>Beschreibung:</td>
         <td><textarea name="te_zweck" rows=5 cols=60><? echo "$te_zweck"; ?></textarea></td>
      </tr>
   </table>
   <table class="indent"><tr><td>
<?
   ButtonSubmit("Speichern", "Temp");
?>
   </td></tr></table>
</form>
<?php
}

function TempSave() {
	global $menu;
	$te_name = $_REQUEST['te_name'];
	$te_zweck = $_REQUEST['te_zweck'];

	if (!isset($te_name) || strlen($te_name) <= 0) {
	   Error("Sie m&uuml;ssen unbedingt einen Templatenamen angeben!<br>");
	   return -1;
	}

	if (!isset($te_zweck))
	   $te_zweck = "";

	$db = OpenDB();
	$query = "select co_tempidx from counter";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return -1;
	}

	$data = fetchDB($result, 0);
	$co_tempidx = $data[0] + 1;

	QueryDB($db, "begin");
	$query = "insert into tempidx (te_num, te_name, te_zweck) values ";
	$query .= "($co_tempidx, '$te_name', '$te_zweck')";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return -1;
	}

	$query = "update counter set co_tempidx = $co_tempidx";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return -1;
	}

	QueryDB($db, "commit");
	return $co_tempidx;
}

function TempUpdate() {
	global $menu;
	$te_num = $_REQUEST['te_num'];
	$te_name = $_REQUEST['te_name'];
	$te_zweck = $_REQUEST['te_zweck'];

	if (!isset($te_num) || $te_num <= 0) {
	   Error("Error: Interner Fehler: Seriennummer fehlt!");
	   return false;
	}

	if (!isset($te_name) || strlen($te_name) <= 0) {
	   Error("Sie m&uuml;ssen unbedingt einen Templatenamen angeben!<br>");
	   return false;
	}

	if (!isset($te_zweck))
	   $te_zweck = "";

	$db = OpenDB();
	$query = "update tempidx set te_name = '$te_name', te_zweck='$te_zweck' ";
	$query .= "where te_num = $te_num";

	if (!TQueryDB($db, $query)) {
	   closeDB($db);
	   return false;
	}

	return true;
}

function TempDelAsk($te_num) {
	$headline = $_REQUEST['headline'];
	$menu = $_REQUEST['menu'];
	$nav = "te_num=$te_num&menu=$menu&func=TempDelReal&headline=28";

	if (!isset($te_num) || $te_num <= 0) {
	   Error("Interner Fehler: Es wurde keine Templatenummer &uuml;bergeben!");
	   return false;
	}
?>
<br>
<form action="templates.php" method="post">
<table border=2 class="alarm">
   <tr><td colspan=2><big>Warnung!</big><br><br>
           Sie sind im Begriff alle erfassten Tasks dieses Templates zu
	   l&ouml;schen!<br><br>
	   Wollen Sie wirklich dieses Template mit allen Daten
	   unwiederbringlich l&ouml;schen?<br>
      </td>
   </tr>
   <tr>
      <td><center><input type="button" name="yes" value="L&ouml;schen" onClick="javascript:MoveTo('templates.php','yes=yes&<? echo "$nav"; ?>')"></center></td>
      <td><center><input type="button" name="no" value="Abbruch" onClick="javascript:MoveTo('templates.php','no=no&<? echo "$nav"; ?>')"></center></td>
   </tr>
</table>
</form>
<?php
}

function TempDel() {
	$te_num = $_REQUEST['te_num'];

	if (!isset($te_num) || $te_num <= 0) {
	   Error("Interner Fehler: Es wurde keine Templatenummer &uuml;bergeben!");
	   return false;
	}

	$db = OpenDB();
	QueryDB($db, "begin");
	$query = "delete from ptemp where pt_tnum = $te_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	$query = "delete from tempidx where te_num = $te_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	QueryDB($db, "commit");
	closeDB($db);
	return true;
}

$tempdel = $_REQUEST['tempdel'];
$tempedit = $_REQUEST['tempedit'];
$taskadd = $_REQUEST['taskadd'];
$edittask = $_REQUEST['edittask'];
$deletetask = $_REQUEST['deletetask'];
$edittemp = $_REQUEST['edittemp'];

if (isset($tempdel) && $tempdel > 0)
   $func = "tempdel";

if (isset($tempedit) && $tempedit > 0)
   $func = "tempedit";

if (isset($taskadd) && $taskadd > 0)
   $func = "taskadd";

if (isset($edittask) && $edittask > 0)
   $func = "edittask";

if (isset($deletetask) && $deletetask > 0)
   $func = "deletetask";

if (isset($edittemp) && $edittemp > 0)
   $func = "edittemp";

if ($func == "tempnew") {
   TempNew();
}

if ($func == "edittemp")
   TempNew($edittemp);

if ($func == "tempsave") {
   $num = TempSave();

   if ($num < 0)
      TempNew();
   else
      EditTemplate($num);
}

if ($func == "tempupdate") {
   $te_num = $_REQUEST['te_num'];

   if (!TempUpdate())
      TempNew($te_num);
}

if ($func == "tempdel") {
   TempDelAsk($tempdel);
}

if ($func == "TempDelReal") {
   if (isset($_REQUEST['yes']))
      $x = TempDel();
   else
      $x = false;
?>
<form action="forms.php" method="post">
   <input type="hidden" name="templates" value="templates">
   <input type="hidden" name="menu" value="4">
   <br>
<?
   if ($x) {
?>
   <p class="cry">Template wurde erfolgreich gel&ouml;scht!</p>
<?
   }
?>
   <input type="submit" name="submit" value="Weiter -->">
</form>
<?php
}

if ($func == "tempedit") {
   EditTemplate($tempedit);
}

if ($func == "GetColor") {
   GetColor();
}

if ($func == "SaveTask") {
   if (SaveTask()) {
      $pt_tnum = $_REQUEST['pt_tnum'];
      EditTemplate($pt_tnum);
   } else {
      $pt_tnum = $_REQUEST['pt_num'];
      EditTask($pt_tnum, true);
   }
}

if ($func == "taskadd")
   EditTask($taskadd, true);

if ($func == "edittask")
   EditTask($edittask, false);

if ($func == "deletetask")
   AskDeleteTask($deletetask);

if ($func == "DeleteTask") {
   if (isset($_REQUEST['yes'])) {
      DeleteTask();
   }

   $pt_tnum = $_REQUEST['pt_tnum'];
   EditTemplate($pt_tnum);
}

require('footer.inc');
?>
